const request = require('request');
const config  = require('../config/config');

class jungle_scout{
    constructor() {
    }

    async validCookie(cookie){
        return new Promise((resolve, reject) => {
            let options = {
                url:'https://api.junglescout.com/api/global_data',
                method:'GET',
                json:true,
                proxy:`http://${config.PROXY.HOST}:${config.PROXY.PORT}`,
                headers: {
                    "authorization":'Bearer ' + cookie,
                    "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
                }
            };

            request(options,async (err,response,body) => {
                if(err){
                    console.log(`jungle_scout cookie invalid. error:${JSON.stringify(err)}`);
                    if(err.code === 'ECONNRESET'){
                        resolve(true);
                    }else{
                        resolve(false);
                    }
                }else{
                    if(body.error){
                        console.log(`jungle_scout cookie invalid. Response:${JSON.stringify(body)}`);
                        resolve(false);
                    }else{
                        if(body.data){
                            console.log(`jungle_scout cookie invalid pass... userName:${body.data.user.email}`);
                            resolve(true);
                        }else{
                            resolve(false)
                        }
                    }
                }
            })
        })
    }
}

module.exports = jungle_scout;

